package com.sky.controller.admin;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;

/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-07-10 21:51
 */
@Api(tags = "c菜品相关接口")
@Slf4j
@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 新增菜品接口
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品接口")
    public Result getadd(@RequestBody DishDTO dto){
        log.info("添加相关接口:{}",dto);
        String sky= "dish_"+dto.getCategoryId();
        deletekey(sky);
        dishService.getadd(dto);
        return Result.success();
    }

    /**
     * 分页查询相关接口
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询相关接口")
    @GetMapping("page")
    public Result<PageResult> page(DishPageQueryDTO  dishPageQueryDTO){
        log.info("分页查询");
        PageResult result=dishService.page(dishPageQueryDTO);
        return Result.success(result);
    }

    /**
     * 批量删除菜品相关接口
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品相关接口")
    @DeleteMapping
    public  Result deletedish(  @RequestParam List<Long> ids){
                log.info("批量删除相关接口;{}",ids);
                dishService.deletedish(ids);
                deletekey("dish_*");
                return Result.success();
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    @ApiOperation("根据Id查询接口")
    @GetMapping("/{id}")
    public Result<DishVO> getbyid( @PathVariable Long id ){
        log.info("根据Id查询接口:{}",id);
        DishVO dishVO =dishService.getbyid(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dto
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody  DishDTO dto){
        log.info("修改菜品相关接口:{}",dto);
        dishService.update(dto);
        deletekey("dish_*");
        return Result.success();
    }

    /**
     * 修改售卖状态接口
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("修改售卖状态接口")
    @PostMapping("/status/{status}")
    public  Result updatestatus(@PathVariable Integer status,Long id ){
        log.info("修改售卖状态接口:{},{}",status,id);
        deletekey("dish_*");
        dishService.updatestatus(status,id);
        return Result.success();
    }
    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }
    @Autowired
    private RedisTemplate redisTemplate;
    //创建一个清空存缓存的方法
    /**
     * 清除缓存的方法
     * @param keyspace
     */
    public  void  deletekey(String keyspace){
        //查询所有的键
        Set keys = redisTemplate.keys(keyspace);
        //删除
        redisTemplate.delete(keys);
    }

}
